home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1997 February / EnigmA AMIGA RUN 15 (1997)(G.R. Edizioni)(IT)[!][issue 1997-02][PLANET CD V].iso / enigma / earcd / utility / utilfile / dosummin.lha / DoSomething / DoSomething.c < prev    next >
C/C++ Source or Header  |  1996-11-13  |  5KB  |  192 lines

  1. /* DoSomething.c v1.0 -- a program that undertakes a certain action when
  2.  * a file notify event occurs.
  3.  *
  4.  * Code by Maarten C. ter Mors, July 1996
  5.  */
  6.  
  7.  
  8. #include <exec/types.h>
  9. #include <exec/ports.h>
  10. #include <exec/memory.h>
  11. #include <dos/notify.h>
  12.  
  13. #include <proto/all.h>
  14.  
  15. #include <string.h>
  16. #include <stdlib.h>
  17. #include <stdio.h>
  18.  
  19.  
  20. /// Defines
  21. #define NUMARGS     3
  22. #define ERRHEADER   "DoSomething error"
  23. #define CFGERROR    "Configuration file error"
  24.  
  25. #define TEMPLATE    "FILES/M,ACTION/K,CONFIG/K"
  26.  
  27. #define ARG_FILES   0
  28. #define ARG_ACTION  1
  29. #define ARG_CONFIG  2
  30.  
  31. #define ACTIONPOOLPUDDLESIZE    2048
  32. #define REQUESTPOOLPUDDLESIZE   4096
  33. ///
  34.  
  35.  
  36. /// Global variables
  37. struct MsgPort *NotifyPort;
  38. void *ActionPool,*RequestPool;
  39. ///
  40.  
  41.  
  42.  
  43. BOOL ParseArgs(UBYTE **Files, UBYTE *Action)
  44. /// translates the arguments into notify requests
  45. {
  46.     UBYTE *ActionStr,*FileStr;
  47.     struct NotifyRequest *nreq;
  48.     ULONG i=0,actionlen,filestrlen,notifylen;
  49.     BOOL success=FALSE;
  50.  
  51.     if (! (Files && Action)) return(FALSE);
  52.     notifylen=sizeof(struct NotifyRequest);
  53.  
  54.     ActionStr=(UBYTE *)
  55.         AllocPooled(ActionPool,actionlen=(strlen(Action) + 1));
  56.     if (! ActionStr)
  57.         return(FALSE);
  58.  
  59.     sprintf(ActionStr,"%s",Action);
  60.  
  61.     while (Files[i]) {
  62.         FileStr=(UBYTE *)
  63.             AllocPooled(RequestPool,filestrlen=(strlen(Files[i]) + 1));
  64.         nreq   =(struct NotifyRequest *)
  65.             AllocPooled(RequestPool,notifylen);
  66.  
  67.         if (! (FileStr && nreq)) {
  68.             if (FileStr) FreePooled(RequestPool,FileStr,filestrlen);
  69.             if (nreq)    FreePooled(RequestPool,nreq,notifylen);
  70.             break;
  71.         }
  72.  
  73.  
  74.         strcpy(FileStr,Files[i]);
  75.         nreq->nr_Name    = FileStr;
  76.         nreq->nr_UserData= (ULONG) ActionStr;
  77.         nreq->nr_Flags   = NRF_SEND_MESSAGE;
  78.         nreq->nr_stuff.nr_Msg.nr_Port= NotifyPort;
  79.  
  80.         success= StartNotify(nreq) ? TRUE : success;
  81.         i++;
  82.     }
  83.  
  84.     return(success);
  85. }
  86. ///
  87.  
  88. int main(void)
  89. /// The program entry point, uses ReadArgs() to parse input and starts notify
  90. {
  91.     LONG Arguments[NUMARGS],ConfigArgs[NUMARGS],signal=0L,i;
  92.     struct RDArgs *Args=NULL,*check,ConfigRDArgs;
  93.     BPTR ConfigFile=0L;
  94.     int retcode=0;
  95.     BOOL success=FALSE;
  96.     UBYTE ConfigLine[256],*ExecuteString,SystemString[512];
  97.     struct NotifyMessage *NotifyMsg;
  98.  
  99.  
  100.     for (i=0; i<NUMARGS; i++) Arguments[i]=ConfigArgs[i]=0L;
  101.     Args= ReadArgs(TEMPLATE,Arguments,NULL);
  102.     if (! Args) {
  103.         PrintFault(IoErr(),ERRHEADER);
  104.         retcode=10;
  105.         goto end;
  106.     }
  107.  
  108.     // Allocate memory pools
  109.     ActionPool= CreatePool(MEMF_ANY|MEMF_CLEAR,ACTIONPOOLPUDDLESIZE,ACTIONPOOLPUDDLESIZE);
  110.     RequestPool=CreatePool(MEMF_ANY|MEMF_CLEAR,REQUESTPOOLPUDDLESIZE,REQUESTPOOLPUDDLESIZE);
  111.     if (! (ActionPool && RequestPool)) {
  112.         Printf("%s Can't allocate memory pools !\n",ERRHEADER);
  113.         retcode=20;
  114.         goto end;
  115.     }
  116.  
  117.     // Allocate message port
  118.     NotifyPort=CreateMsgPort();
  119.     if (! NotifyPort) {
  120.         Printf("%s Can't open notify message port !\n",ERRHEADER);
  121.         retcode=20;
  122.         goto end;
  123.     }
  124.  
  125.  
  126.     // Distinguish between normal arguments and config file
  127.     if (Arguments[ARG_CONFIG]) {
  128.         ConfigFile=Open((UBYTE *) Arguments[ARG_CONFIG],MODE_OLDFILE);
  129.         if (! ConfigFile) {
  130.             PrintFault(IoErr(),CFGERROR);
  131.             goto end;
  132.         }
  133.  
  134.         while(FGets(ConfigFile,ConfigLine,255)) {
  135.             ConfigRDArgs.RDA_Source.CS_Buffer=ConfigLine;
  136.             ConfigRDArgs.RDA_Source.CS_Length=255;
  137.             ConfigRDArgs.RDA_Source.CS_CurChr=0;
  138.  
  139.             ConfigRDArgs.RDA_DAList=0L;
  140.             ConfigRDArgs.RDA_Buffer=NULL;
  141.             ConfigRDArgs.RDA_ExtHelp=NULL;
  142.             ConfigRDArgs.RDA_Flags=RDAF_NOPROMPT;
  143.  
  144.             check=ReadArgs(TEMPLATE,ConfigArgs,&ConfigRDArgs);
  145.             if (check) {
  146.                 success=
  147.                     ParseArgs((UBYTE **) ConfigArgs[ARG_FILES], (UBYTE *) ConfigArgs[ARG_ACTION])
  148.                     ? TRUE : success;   // if ParseArgs returns TRUE, make success TRUE,
  149.                                         // else leave old value unchanged.
  150.                 FreeArgs(check);
  151.             }
  152.         }
  153.         Close(ConfigFile);
  154.  
  155.     } else
  156.         success= ParseArgs((UBYTE **) Arguments[ARG_FILES], (UBYTE *) Arguments[ARG_ACTION]);
  157.  
  158. end:
  159.     if (Args) FreeArgs(Args);
  160.  
  161.     if (success) {
  162.         while(signal != SIGBREAKF_CTRL_C) {
  163.             signal=Wait(1L << NotifyPort->mp_SigBit | SIGBREAKF_CTRL_C);
  164.  
  165.             NotifyMsg= (struct NotifyMessage *) GetMsg(NotifyPort);
  166.             if (NotifyMsg) {
  167.                 ExecuteString= (UBYTE *) NotifyMsg->nm_NReq->nr_UserData;
  168.                 if (strstr(ExecuteString,"%s") || strstr(ExecuteString,"%S")) {
  169.                     strcpy(ConfigLine,NotifyMsg->nm_NReq->nr_FullName);
  170.                     AddPart(ConfigLine,NotifyMsg->nm_NReq->nr_Name,512);
  171.                     sprintf(SystemString,ExecuteString,ConfigLine);
  172.                 } else
  173.                     strcpy(SystemString,ExecuteString);
  174.  
  175.                 SystemTags(SystemString, TAG_DONE);
  176.  
  177.                 ReplyMsg((struct Message *) NotifyMsg);
  178.  
  179.             }
  180.         }
  181.     } else
  182.         Printf("%s: Unable to make any notifications.\n",ERRHEADER);
  183.  
  184.  
  185.     if (ActionPool) DeletePool(ActionPool);
  186.     if (RequestPool) DeletePool(RequestPool);
  187.  
  188.     exit(retcode);
  189. }
  190. ///
  191.  
  192.